package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.base.entity.CalcinationPurificationBleaching;
import com.hkts.naturalgas.base.entity.RoastingPurificationBleaching;
import com.hkts.naturalgas.base.entity.RoastingPurifyWetElectricity;
import com.hkts.naturalgas.base.mapper.RoastingPurifyWetElectricityMapper;
import com.hkts.naturalgas.base.service.IRoastingPurifyWetElectricityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class RoastingPurifyWetElectricityServiceImpl extends ServiceImpl<RoastingPurifyWetElectricityMapper, RoastingPurifyWetElectricity> implements IRoastingPurifyWetElectricityService {


    @Autowired
    private RoastingPurifyWetElectricityMapper roastingPurifyWetElectricityMapper;

    @Override
    public IPage<RoastingPurifyWetElectricity> selectPage(Page<RoastingPurifyWetElectricity> page, RoastingPurifyWetElectricity roastingPurifyWetElectricity) {
        QueryWrapper<RoastingPurifyWetElectricity> query = Wrappers.query();
        QueryWrapper<RoastingPurifyWetElectricity> entityWrapper = getEntityWrapper(query, roastingPurifyWetElectricity);
        return roastingPurifyWetElectricityMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<RoastingPurifyWetElectricity> selectList(RoastingPurifyWetElectricity roastingPurifyWetElectricity) {
        QueryWrapper<RoastingPurifyWetElectricity> query = Wrappers.query();
        QueryWrapper<RoastingPurifyWetElectricity> entityWrapper = getEntityWrapper(query, roastingPurifyWetElectricity);
        return roastingPurifyWetElectricityMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<RoastingPurifyWetElectricity> getEntityWrapper(QueryWrapper<RoastingPurifyWetElectricity> entityWrapper, RoastingPurifyWetElectricity roastingPurifyWetElectricity) {
        //条件拼接
        return entityWrapper;
    }

    @Override
    public IPage<RoastingPurifyWetElectricity> selectPageByTime(Page<RoastingPurifyWetElectricity> page, String startDate, String endDate) {
        QueryWrapper<RoastingPurifyWetElectricity> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(RoastingPurifyWetElectricity::getTime);
        IPage<RoastingPurifyWetElectricity> hktsTailGasPage = roastingPurifyWetElectricityMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<RoastingPurifyWetElectricity> getBrokenLineGraph(Page<RoastingPurifyWetElectricity> page,String startDate, String endDate){
        QueryWrapper<RoastingPurifyWetElectricity> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(RoastingPurifyWetElectricity::getTime);
        IPage calcinationDesulfurizationDenitrations = roastingPurifyWetElectricityMapper.selectPage(page,query);
        List<RoastingPurifyWetElectricity> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(RoastingPurifyWetElectricity vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("焙烧二电场二次电流(A)",vo.getBsjhSd2i2());
        data.put("焙烧二电场二次电压(V)",vo.getBsjhSd2v2());
        data.put("焙烧一电场二次电流(A)",vo.getBsjhSdi2());
        data.put("焙烧一电场二次电压(V)",vo.getBsjhSdv2());
        data.put("time",vo.getTime());
        return data;
    }
    @Override
    public void deleteAll() {
        roastingPurifyWetElectricityMapper.deleteAll();
    }

    @Override
    public void importByFiles(List<RoastingPurifyWetElectricity> list) {
        this.saveBatch(list);
    }


}
